﻿using csharp_Movies_315.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace csharp_Movies_315.Controllers
{
    public class TodoController : Controller
    {
        // GET: Todo
        //public ActionResult Index()
        //{
        //    return View();
        //}

        //READ
        public ActionResult Home()
        {
            // create vm
            TodoHomeVm homeViewModel = new TodoHomeVm();
            // populate vm Greeting
            homeViewModel.Greeting = "welcome guen thage";
            // populate vm Todos
            homeViewModel.Todos = TodoData.GetTodos();
            // send vm to view
            return View(homeViewModel);
        }

        // CREATE
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(TodoCreateVm tcVm)
        {
            //create a new Todo from arg 
            Todo nTodo = new Todo()
            {
                TodoId = TodoData.AssignCount(),
                IsCompleted = false,
                Task = tcVm.Task
            };
            //save it to Data
            TodoData.AddTodo(nTodo);

            // redirect the user..
            return RedirectToAction("Home");
        }
        // DELETE
        public ActionResult Delete(int id)
        {
            // delete based on List Index
            TodoData.DeleteTodo(id);
            // redirect the user..
            return RedirectToAction("Home");
        }

        // UPDATE
        [HttpGet]
        public ActionResult Update(int id)
        {
            // create vm
            TodoUpdateVm tuVm = new TodoUpdateVm();
            // populate it, from TodoData
            Todo tempTodo = TodoData.GetTodo(id);
            tuVm.TodoUpdate = tempTodo;
            // send to view
            return View(tuVm);
        }
         [HttpPost]
        public ActionResult Update(TodoUpdateVm uVm)
        {
             // create a new todo 
            Todo editedTodo = new Todo()
            {
                TodoId = uVm.TodoUpdate.TodoId,
                Task = uVm.TodoUpdate.Task,
                IsCompleted = uVm.TodoUpdate.IsCompleted
            };
             // save it back to the DataContainer
            TodoData.SaveTodo(editedTodo);

            return RedirectToAction("Home");
        }



    }
}